<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ChangeStateController extends Controller
{
    //

    /**
     * 修改状态
     *
     * @param Request $request
     * @param $table 修改表
     * @param $field 修改字段
     * @return array
     */
    public function changeState(Request $request, $table, $field)
    {
        $id = $request -> get('id');
        $obj = DB::table($table)
            ->where('id', $id)
            ->first($field);
        $status = $obj->$field == 1 ? 0 : 1;

        $result  = DB::table($table)
            ->where('id', $id)
            ->update([ $field => $status ]);
        if ($result) {
            return $this->res_msg(0, '修改成功');
        } else {
            return $this->res_msg(1, '修改失败');
        }
    }
}
